package com.liukai.exam.service.impl;

import com.liukai.exam.pojo.BigQuestion;
import com.liukai.exam.mapper.BigQuestionMapper;
import com.liukai.exam.pojo.exam.BigQuestionManage;
import com.liukai.exam.service.IBigQuestionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liukai.exam.utils.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 每个试卷的大题有哪些，存放第几大题的题目类型 服务实现类
 * </p>
 *
 * @author liu kai
 * @since 2021-02-01
 */
@Service
public class BigQuestionServiceImpl extends ServiceImpl<BigQuestionMapper, BigQuestion> implements IBigQuestionService {

    @Autowired
    private BigQuestionMapper bigQuestionMapper;

    /**
     * 根据examId新增大题类型
     *
     * @param bigQuestionManage
     * @return
     */
    @Override
    public int addBigQuestion(BigQuestionManage bigQuestionManage) {

        // 新增成功次数
        int count = 0;
        Long examId = bigQuestionManage.getExamId();
        List<Integer> bigQuestionIndex = bigQuestionManage.getBigQuestionIndex();
        // 需要增加的大题数目
        int size = bigQuestionIndex.size();
        String examName = "";
        Integer examIndex = -1;
        BigQuestion bigQuestion;
//        将所有的大题一次添加进入数据库
        for (Integer questionIndex : bigQuestionIndex) {
            bigQuestion = new BigQuestion();
            examName = Constant.getQueNameByIndex(questionIndex);
            examIndex = questionIndex;
            bigQuestion.setBigQuestionIndex(examIndex);
            bigQuestion.setBigQuestionName(examName);
            bigQuestion.setExamId(examId);
            count += bigQuestionMapper.addBigQuestion(bigQuestion);
        }
        return count;
    }

    /**
     * 根据examId查找试卷大题信息
     *
     * @param examId
     * @return
     */
    @Override
    public List<BigQuestion> findBigQueById(Long examId) {
        return bigQuestionMapper.findBigQueById(examId);
    }
}
